package com.naizhuai.ui.mianbusiness.setting;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.entity.QRCodeMsgBean;
import com.naizhuai.ui.entity.QRCodeScanBean;
import com.naizhuai.util.BitmapUtils;
import com.naizhuai.util.DeviceUtils;
import com.naizhuai.util.DownLoadConstants;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.IntentUtils;
import com.naizhuai.util.NaizhuAILog;
import com.naizhuai.util.QRCodeUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 生成的二维码下载界面
 */
public class ApkDownQRCodeActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "ApkDownQRCodeActivity";

    @BindView(R.id.image_qr_code)
    ImageView qrCodeImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBaseView();
    }

    public static void startActivity(Context mContext) {
        IntentUtils.startActivity(mContext, ApkDownQRCodeActivity.class);
    }

    private void initBaseView() {
        qrCodeImage.post(new Runnable() {
            @Override
            public void run() {
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) qrCodeImage.getLayoutParams();
                int width = qrCodeImage.getWidth();
                params.height = width;
                qrCodeImage.setLayoutParams(params);
                initView(width);
            }
        });
    }

    private void initView(int size) {
        new BitmapThread().start();
        String downloadUrl = getShareValue(DownLoadConstants.DOWNLOAD_APK_URL_KEY, DownLoadConstants.DOWNLOAD_APK_URL);
        String url = "";
//        if(TextUtils.isEmpty(downloadUrl)){
        url = DownLoadConstants.DOWNLOAD_APK_URL;
//        }else
//            url=downloadUrl;
        urlHandler = new UrlHandler(qrCodeImage, size, url, this);
    }

    @OnClick({R.id.title_delete_icon})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_delete_icon:
                super.onBackPressed();
                break;
        }
    }

    public class BitmapThread extends Thread {

        @Override
        public void run() {
            Bitmap bitLogo = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
            urlHandler.setBitmap(bitLogo);
            urlHandler.sendEmptyMessage(0);
        }
    }

    private UrlHandler urlHandler;

    static class UrlHandler extends Handler {
        private Context mContext;
        private String content;
        private ImageView qrCodeImage;
        private int size;
        private Bitmap urlBit;

        public UrlHandler(ImageView qrCodeImage, int size, String content, Context mContext) {
            this.qrCodeImage = qrCodeImage;
            this.size = size;
            this.content = content;
            this.mContext = mContext;
        }

        public void setBitmap(Bitmap bitLogo) {
            this.urlBit = bitLogo;
        }

        @Override
        public void handleMessage(Message msg) {
            Bitmap scanLogoBit = BitmapUtils.getRoundBorderBitmap(urlBit, DeviceUtils.floatDp2px(mContext, 40f), DeviceUtils.floatDp2px(mContext, 40f), DeviceUtils.dp2px(mContext, 5));
            Bitmap scanBit = QRCodeUtil.createQRCodeWithLogo(content, size, scanLogoBit);
            if (scanBit != null)
                qrCodeImage.setImageBitmap(scanBit);
            else
                qrCodeImage.setImageResource(R.mipmap.wrong_image_icon);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_qr_download_layout;
    }

}
